﻿using System.ComponentModel;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web.Mvc;
using Web.Models;
using Web.Models.ViewModels;


namespace Web.Areas.ManagerArea.Controllers
{
    [Description("CategoryController说明")]
    public class CategoryController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(int? parentId, int page = 1, int pageSize = 20)
        {
            DbDataResult result = new DbDataResult();
            using (var db = MyDbContext.Create())
            {
                var list = db.Categories.Where(m => !m.IsDeleted).OrderBy(m => m.OrderId);
                result.CurrentPage = page;
                result.PageSize = pageSize;
                result.RecordCount = list.Count();
                result.Data = list.ToList();
                result.Message = "success";
                result.StatusCode = 0;
            }
            return new LowerCamelJsonResult(result);
        }

        public ActionResult Edit(int? id)
        {
            using (var db = MyDbContext.Create())
            {
                var t = db.Categories.Where(m => !m.IsDeleted).OrderBy(m => m.OrderId).ToList();
                ViewBag.ParentId = t;
                if (null == id) return View();
                var model = t.SingleOrDefault(m => m.Id == id.Value);
                if (null == model) return HttpNotFound();
                return View(model);
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Edit(Category model)
        {
            DbResult result = new DbResult();
            if (ModelState.IsValid)
            {
                using (var db = MyDbContext.Create())
                {
                    if (null != model.ParentId)
                    {
                        var parent = db.Categories.Find(model.ParentId);
                        if (null == parent)
                        {
                            result.Message = "父级错误，未找到指定父级";
                            return new LowerCamelJsonResult(result);
                        }
                        //model.ParentIds = $",{parent.ParentIds?.Trim(',')},{parent.Id},";
                        if (string.IsNullOrEmpty(parent.ParentIds))
                        {
                            model.ParentIds = $",{parent.Id},";
                        }
                        else
                        {
                            model.ParentIds = $",{parent.ParentIds.Trim(',')},{parent.Id}";
                        }
                    }
                    db.Categories.AddOrUpdate(model);
                    db.SaveChanges();
                    result.Message = "success";
                    result.StatusCode = 0;
                }
            }
            else
            {
                AddErrors(out result);
            }
            return new LowerCamelJsonResult(result);
        }
    }
}
